﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using TFSClient = Microsoft.TeamFoundation.WorkItemTracking.Client;

using LeMigue.Scrum.TfsScrumV1.Utils;
using LeMigue.Scrum.Iterface.Service;
using LeMigue.Scrum.Iterface.Model;

namespace LeMigue.Scrum.TfsScrumV1.Service
{
    public class BugService : AbstractTfsService, IBugService
    {

        private ITaskService _TaskService = new TaskService();

        private WorkItemFields[] QuerySelect = new WorkItemFields[] { };

        public IList<Bug> List(Team team)
        {
            throw new NotImplementedException();
        }

        public IList<Bug> List(Sprint sprint)
        {
            return List(sprint.Iteration);
        }
        public IList<Bug> List(Iteration iteration)
        {
            TfsQueryItem query = new TfsQueryItem(QuerySelect);

            query.AppendWhere(WorkItemFields.IterationId, "=", iteration.Id)
                 .AppendWhere(" AND ").AppendWhere(WorkItemFields.WorkItemType, "=", WorkItemType.Bug.ToString());


            return List(GetItens(query));
        }
        public IList<Bug> List(ItemBacklog backlog)
        {
            throw new NotImplementedException();
        }

        private IList<Bug> List(TFSClient.WorkItemCollection workItemCollection)
        {
            IList<Bug> bugs = new List<Bug>();
            foreach (TFSClient.WorkItem item in workItemCollection)
            {
                if (item.WorkItemLinks.Count == 0 || !item.WorkItemLinks.ContainParent(WorkItemLinkType.Parent.ToString()))
                {
                    Bug bug = item.ConvertToScrumItem<Bug>(CreateBug);
                    bugs.Add(bug);
                }
            }

            return bugs;
        }
        private IList<Bug> List(IList<TFSClient.WorkItem> listWorkItem)
        {
            IList<Bug> bugs = new List<Bug>();
            foreach (TFSClient.WorkItem item in listWorkItem)
            {
                if (item.WorkItemLinks.Count == 0 || !item.WorkItemLinks.ContainParent(WorkItemLinkType.Parent.ToString()))
                {
                    Bug bug = item.ConvertToScrumItem<Bug>(CreateBug);
                    bugs.Add(bug);
                }
            }

            return bugs;
        }

        public Bug Load(int id)
        {
            return GetItem(id).ConvertToScrumItem<Bug>(CreateBug);
        }

        public Bug Save(Bug item)
        {
            Save(item.ConvertToWorkItem<Bug>(GetItem(item.Id), SaveBug));

            return item;
        }

        #region workitem

        protected Bug CreateBug(TFSClient.WorkItem w, Bug item)
        {

            item.SetTasks(new System.Lazy<IList<Task>>(() => _TaskService.List(item)));

            TFSClient.Node no = w.Project.IterationRootNodes.GetNode(w.IterationId);
            if (no != null)
            {
                item.Iteration = new Iteration(no.Id, no.Name);
            }

            return item;
        }

        protected TFSClient.WorkItem SaveBug(Bug item, TFSClient.WorkItem w)
        {
            return w;
        }
        #endregion




        
    }
}
